<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="历史版本" width="1100px" destroyOnClose>
    <BasicTable @register="registerTable">
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>

    <Detail @register="registerDetail" />
  </BasicModal>
</template>
<script lang="ts" name="oauth-client" setup>
  import { ref } from "vue"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useGlobSetting } from "/@/hooks/setting"
  import { useDrawer } from "/@/components/Drawer"
  import { historyColumns } from "./data"
  import Detail from "./Detail.vue"
  import { listHistoryClient, deleteOauthClient } from "./api"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { useMessage } from "/@/hooks/web/useMessage"
  import { BasicModal, useModalInner } from "/@/components/Modal"
  const { createMessage } = useMessage()

  const clientInfo = ref<any>({})
  const glob = useGlobSetting()
  const [registerDetail, { openDrawer: openDetail }] = useDrawer()
  const { prefixCls, tableContext } = useListPage({
    designScope: "oauth-history-client",
    tableProps: {
      api: listHistoryClient,
      columns: historyColumns,
      beforeFetch: (params) => {
        params.clientId = clientInfo.value.clientId
      },
      useSearchForm: false,
      immediate: false,
      bordered: false,
      showTableSetting: false,
      canResize: false,
      pagination: false,
      actionColumn: {
        width: 100,
        title: "操作",
        align: "center",
        dataIndex: "action",
        slots: { customRender: "action" },
      },
    },
  })

  const [registerTable, { reload }] = tableContext

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({ footer: null })
    clientInfo.value = data.record
    reload()
  })
  async function handleDelete(record) {
    await deleteOauthClient({ id: record.id }, reload)
  }

  const handleOpenDetails = (record) => openDetail(true, { record })

  function getActions(record): any[] {
    return [
      {
        label: "详情",
        onClick: handleOpenDetails.bind(null, record),
      },
      {
        label: "删除",
        ifShow: record.sendStatus != 1,
        color: "error",
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }

  function getDropDownAction(record) {
    return []
  }
</script>
